package pl.edu.pw.ee.buildcity.notifiers;

import org.apache.commons.lang.StringUtils;
import org.apache.velocity.app.VelocityEngine;
import pl.edu.pw.ee.buildcity.entities.BuildActivity;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author Mateusz
 * @since 2010-02-14
 */
public abstract class AbstractNotifier {

	public static final String SEPARATOR = ";";

	@Resource(name = "velocityEngine")
	protected VelocityEngine velocityEngine;

	/**
	 * Creates appropriate message based on template and sends it to given list of recipients.
	 *
	 * @param recipients    list of recipients, separated by {@link AbstractNotifier#SEPARATOR} (i.e. me@mail.com;you@mail.com)
	 * @param buildActivity
	 * @throws CouldNotNotifyException when current recipient could not be notified (i.e. invalid email or GG uin)
	 */
	public void notifyEndOfBuild(String recipients, BuildActivity buildActivity) throws CouldNotNotifyException {
		List<String> listOfRecipients = rewriteRecipientsIntoList(recipients);
		notifyEndOfBuild(listOfRecipients, buildActivity);
	}

	private List<String> rewriteRecipientsIntoList(String recipients) {
		List<String> listOfRecipients = Collections.emptyList();
		if (StringUtils.isNotBlank(recipients)) {
			listOfRecipients = Arrays.asList(recipients.split(SEPARATOR));
		}
		return listOfRecipients;
	}

	protected abstract void notifyEndOfBuild(List<String> recipients, BuildActivity buildActivity) throws CouldNotNotifyException;

}
